package com.it.emall.service.impl;

import com.it.emall.domain.Ingredients;
import com.it.emall.domain.Recipes;
import com.it.emall.mapper.RecipeMapper;
import com.it.emall.service.RecipesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RecipesServiceImpl implements RecipesService {

    @Autowired
    private RecipeMapper recipeMapper;

    //食谱列表
    @Override
    public List<Recipes> selectIngredientsList() {
           return recipeMapper.selectRecipes(null,null);
    }

    //搜索
    @Override
    public List<Recipes> searchRecipesByKeyword(String search) {
        // 调用 Mapper 进行模糊查询
        return recipeMapper.searchRecipesByKeyword(search);
    }


    @Override
    public Recipes selectRecipesById(Integer id) {
        return recipeMapper.selectRecipesById(id);
    }


    @Override
    public Recipes selectRecipesByIngredientId(Integer ingredientId) {
        return recipeMapper.selectRecipesByIngredientId(ingredientId);
    }

    //增加食谱
    @Override
    public int addRecipe(Recipes recipes,String ingredientName, String ingredientDesc, String picUrl, String iconUrl,
                         int l1CategoryId) {
        // 先在 ingredients 表插入 L2 级别的食材，并获取生成的 ID
        Ingredients newIngredient = new Ingredients();
        newIngredient.setName(ingredientName);
        newIngredient.setDesc(ingredientDesc);
        newIngredient.setPid(l1CategoryId);  // 设定 L1 分类的 ID 作为 PID
        newIngredient.setPicUrl(picUrl);
        newIngredient.setIconUrl(iconUrl);
        int insertCount = recipeMapper.addIngredient(newIngredient);
        if (insertCount == 0 || newIngredient.getId() == null) {
            throw new RuntimeException("添加 L2 级别的食材失败");
        }
        // 获取新创建的 L2 分类 ID
        int newL2Id = newIngredient.getId();


        // 将 L2 ID 存入食谱表
        recipes.setIngredientId(newL2Id);

        return recipeMapper.addRecipes(recipes);

    }

    //修改食谱
    @Transactional // 事务保证两步都成功
    @Override
    public int updateRecipeAndSyncCategory(Recipes recipe, Integer pid) {
        // 先更新食谱表
        int recipeUpdateCount = recipeMapper.updateRecipe(recipe);

        // 再同步更新食材表
        int ingredientUpdateCount = recipeMapper.updateIngredientsCategory(recipe.getId(), pid);
        System.out.println(recipe.getId());
        System.out.println(pid);

        return recipeUpdateCount + ingredientUpdateCount;
    }
    //删除食谱
    @Override
    public int deleteRecipeById(Integer id, Integer userId) {
        int i1 = recipeMapper.deleteIngredientByRecipeId(id, userId);
        int i = recipeMapper.deleteRecipeById(id, userId);
        return i1+i;
    }

    //根据userid查询用户发布的食谱
    @Override
    public List<Recipes> selectRecipesByuserId(Integer userId) {
        return recipeMapper.getRecipesByUserId(userId);
    }

    //查询用户发布食谱的详情页 用于修改食谱`
    @Override
    public Recipes selectRecipesByuserIdRecipe(Integer userId, Integer id) {
        System.out.println(userId);
        System.out.println(id);
        return recipeMapper.selectRecipesByuserIdUpdate(id,userId);
    }


}
